<!-- 红包商城 商品分类管理 -->
<template>
  <div class="table">
    <div class="container">
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>
            <el-icon>
              <Search />
            </el-icon> 查询条件：
          </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row :gutter="10">
        <el-col :xl="6" :lg="6" :md="12" :sm="24" style="margin-bottom:10px">
          <el-input v-model.trim="searchCondition.name" placeholder="分类名称" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" style="margin-bottom:10px">
          <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
          <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
        </el-col>
      </el-row>
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>查询结果：</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="handle-box">
        <el-button type="primary" :icon="Plus" class="mr10" :disabled="disabledbtn" @click="editVisible = true">新增
        </el-button>
        <el-popconfirm placement="top" width="160" title="你确定要删除吗?" @confirm="batchDelete('deleteMallCategoryInfo')">
          <template #reference>
            <el-button type="danger" :icon="Delete" class="mr10" :disabled="disabledbtn">批量删除
            </el-button>
          </template>
        </el-popconfirm>
      </div>
      <el-table :data="tableState.tableList.slice((pageNumber - 1) * pageSize, pageNumber * pageSize)" border
        class="table" ref="multipleTableRef" v-loading="tableLoading" :reserve-selection="true"
        @row-dblclick="rowDblclick">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="分类名称" align="center">
        </el-table-column>
        <el-table-column prop="status" label="页面上分类是否显示" align="center" :formatter="handlFormatterStatus">
        </el-table-column>
        <el-table-column prop="status" label="金刚区是否显示" align="center" :formatter="handlFormatterKingKongDistrictStatus">
        </el-table-column>
        <el-table-column prop="sort" label="排序" align="center">
          <template #default="scope">
            <div class="">
              <el-input v-model.trim.number="scope.row.sort" placeholder="请输入内容"
                @change="handleSort(scope.row)"></el-input>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" size="small" text :icon="View" @click="handleViewEdit(scope.row, 'view', '查看')">
              查看
            </el-button>
            <el-button type="primary" size="small" text :icon="Edit" @click="handleViewEdit(scope.row, 'edit', '编辑')">
              编辑
            </el-button>
            <div class="display-inline-block" v-permission="'delete'">
              <el-popconfirm placement="top" width="160" title="你确定要删除吗?"
                @confirm="deleteInfo('deleteMallCategoryInfo', [id])">
                <template #reference>
                  <el-button text :icon="Delete" size="small" class="red" @click="id = scope.row.id">删除
                  </el-button>
                </template>
              </el-popconfirm>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background @update:current-page="handleCurrentChange" @update:page-size="handleSizeChange"
          layout="total, sizes, prev, pager, next, jumper" :page-size="pageSize" :current-page="pageNumber"
          :page-sizes="pageSizes" :total="total" :hide-on-single-page="false">
        </el-pagination>
      </div>
    </div>

    <!-- 新增编辑查看弹出框 -->
    <el-dialog :title="dialogTitle" v-model="editVisible" width="60%" draggable
      :close-on-click-modal="closeOnClickModal" @closed="closedFormDialog">
      <el-form ref="categoriesInfoFormRef" :model="categoriesInfoForm" :rules="rules" v-loading="formLoading"
        label-width="150px">
        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="分类名称：" prop="name">
              <el-input v-model.trim="categoriesInfoForm.name" placeholder="分类名称">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="排序：" prop="sort">
              <el-input v-model.trim.number="categoriesInfoForm.sort" placeholder="排序"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="10">
            <el-form-item label="是否显示：" prop="status">
              <el-switch v-model="categoriesInfoForm.status" :active-value='1' :inactive-value='0' active-text="显示"
                inactive-text="隐藏">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="是否在金刚区显示：" prop="is_vajra">
              <el-switch v-model="categoriesInfoForm.is_vajra" :active-value='1' :inactive-value='0' active-text="显示"
                inactive-text="隐藏">
              </el-switch>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="图片：" prop="image">
              <div class="w-spec">
                <!-- accept解决源头上传资源的格式 -->
                <el-upload class="avatar-uploader" v-if="!categoriesInfoForm.image" :action="uploadUrl"
                  :data="uploadData" accept=".png,.jpg,.jpeg" :show-file-list="false"
                  :on-success="(response) => handleResourcesUploadSuccess(response, categoriesInfoForm, 'image')"
                  :before-upload="(file) => handleBeforeUpload(file)">
                  <el-icon class="el-icon-plus avatar-uploader-icon">
                    <Plus />
                  </el-icon>
                </el-upload>
                <div class="wzg-continer-img" v-else>
                  <img :src="categoriesInfoForm.image === '' ? '' : imageUrl + categoriesInfoForm.image" alt="规格图片"
                    class="wzg-img">
                  <el-icon class="el-icon-close" @click="() => handleDeleteResources(categoriesInfoForm, 'image')">
                    <Close />
                  </el-icon>
                </div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="1">
            <el-tooltip placement="top" content="显示在金刚区中的图片需要上传">
              <el-icon class="icon-wzg-config">
                <InfoFilled />
              </el-icon>
            </el-tooltip>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="editVisible = false">取 消</el-button>
          <el-button type="primary" :disabled="disabledbtn" v-if="!isDisabledbtn"
            @click="submitForm('commonMallCategoryInfo', getTableInfo)">确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import { reactive, ref, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { View, Delete, Edit, Search, Plus, Refresh } from '@element-plus/icons-vue';
import { api as Request, imageUrl, uploadUrl } from '@/utils/api';
import { useTable } from '@/hooks/useTable.js';
import { useDialogForm } from '@/hooks/useDialogForm.js';
import { useSort } from '@/hooks/useSort.js';
import { useUpload } from '@/hooks/useUpload.js';
import useRules from '@/hooks/useRules.js';

const disabledbtn = ref(false);/* 按钮可用与不可用 */
const formLoading = ref(false);/* 表单loading状态 */
let { total, pageNumber, pageSize, pageSizes, multipleTableRef, tableLoading, tableState, searchCondition,
  handleCurrentChange, handleSizeChange, getTableInfo, searchInfo, resetSearch, batchDelete, deleteInfo, rowDblclick } =
  useTable({ name: '' }, 'getMallCategoryInfo', disabledbtn, 'FRONT_END');

let { formRef: categoriesInfoFormRef, formData: categoriesInfoForm, methodType, isDisabledbtn,
  closeOnClickModal, dialogTitle, editVisible, closedFormDialog, submitForm } = useDialogForm({
    id: '',
    image: '',
    name: '',
    is_vajra: 0,
    sort: 0,
    status: 1
  }, disabledbtn, handleCustomOperation, formLoading);

let { handleSort } = useSort('putMallCategorySortInfo', searchInfo);
let { uploadData,
  handleDeleteResources, handleResourcesUploadSuccess, handleBeforeUpload } = useUpload(disabledbtn, formLoading);

const rules = useRules('categoriesRules');/* 校验规则 */

const id = ref(-1);/* 删除时所需的编号 删除时的id */

/* 格式化是否显示 */
const handlFormatterStatus = (row) => {
  return row.status === 1 ? '显示' : row.status === 0 ? '隐藏' : '未知';
}
/* 格式化金刚区是否显示 */
const handlFormatterKingKongDistrictStatus = (row) => {
  return row.is_vajra === 1 ? '显示' : row.is_vajra === 0 ? '隐藏' : '未知';
}

/* 点击编辑按钮 */
const handleViewEdit = (row, code, name) => {
  if ('view' === code) {
    dialogTitle.value = name;
    isDisabledbtn.value = true;
    closeOnClickModal.value = true;
  } else if ('edit' === code) {
    dialogTitle.value = name;
    methodType.value = 'PUT';
  }
  /* nextTick 解决重置该表单项 初始化的数据问题 */
  nextTick(() => {
    categoriesInfoForm.id = row.id;
    categoriesInfoForm.image = row.image;
    categoriesInfoForm.name = row.name;
    categoriesInfoForm.sort = row.sort;
    categoriesInfoForm.status = row.status;
    categoriesInfoForm.is_vajra = row.is_vajra;
  });
  editVisible.value = true;
}

/*关闭模态框以后的自定义操作 */
function handleCustomOperation () {
  categoriesInfoForm.id = '';/* 这个还需要手动清除 */
  console.log(categoriesInfoForm);
}

getTableInfo();
</script>

<style lang="scss" scoped>
/* layout-布局 */
.el-row {
  margin-bottom: 20px;
}

.el-row:last-child {
  margin-bottom: 0;
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.textAlignCenter {
  text-align: center;
}

/* 修改select的盒模型 */
.el-select {
  display: block;
}

/* 确定按钮的显示与隐藏 */
.displaybtn {
  display: none;
}

/* 修改该属性值  查询条件，查询结果得样式 */
.el-breadcrumb {
  border-bottom: 1px solid #ebeef5;
  padding-bottom: 10px;
}

/* 提示图标设置 */
.icon-wzg-config {
  height: 30px;
  line-height: 30px;
}

/* 修改Switch开关 */
.el-switch {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

/* 图片上传 start */
.w-spec .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.w-spec :deep(.el-upload--text) {
  width: inherit;
}

.w-spec .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.w-spec .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 70px;
  height: 70px;
  line-height: 70px;
  text-align: center;
}

.w-spec .avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.w-spec .avatar-uploader :deep(.el-upload--text) {
  height: inherit;
  padding: 2px 2px;
  color: #409eff;
}

.w-spec .wzg-continer-img {
  position: relative;
  display: inline-block;
  width: 70px;
  height: 70px;
  line-height: 70px;
  text-align: center;

  .wzg-img {
    width: inherit;
    height: inherit;
  }

  .el-icon-close {
    display: none;
  }
}

/* 规格删除按钮 start*/
.wzg-continer-img:hover .el-icon-close {
  position: absolute;
  top: 1px;
  right: -1px;
  z-index: 1000;
  display: inline-block;
  width: 24px;
  height: 24px;
  padding: 5px;
  cursor: pointer;
  background-color: rgba($color: #f00, $alpha: 0.8);
  color: #fff;
  border-radius: 2px;
  transition: 0.5s;
}

.w-spec .el-icon-close:hover,
.w-spec .wzg-video:hover {
  opacity: 0.8;
}

.w-spec .wzg-video {
  background-color: #fff;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
}
</style>
